package com.flyduck.mall.ums.h5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.flyduck.mall.ums.domain.UmsAddress;
import com.flyduck.mall.ums.mapper.UmsAddressMapper;

import com.flyduck.mall.common.core.utils.BeanUtils;

import com.flyduck.mall.ums.h5.service.H5AddressService;
import com.flyduck.mall.ums.h5.vo.H5AddressDetailsRespVO;
import com.flyduck.mall.ums.h5.vo.H5AddressRespVO;
import com.flyduck.mall.ums.h5.vo.H5AddressSaveReqVO;
import com.flyduck.mall.ums.h5.vo.H5AddressUpdateReqVO;
import com.flyduck.ss.common.core.exception.ServiceException;
import com.flyduck.ss.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * AppAddressServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2024-06-19
 */
@Service
public class H5AddressServiceImpl implements H5AddressService {

    @Resource
    private UmsAddressMapper umsAddressMapper;

    @Override
    public List<H5AddressRespVO> getAddressList() {

        List<UmsAddress> umsAddressList = umsAddressMapper.selectList(
                new LambdaQueryWrapper<UmsAddress>()
                        .eq(UmsAddress::getUserId, LoginHelper.getUserId())
                        .orderByDesc(UmsAddress::getCreateTime)
        );
        return BeanUtils.toBeanList(umsAddressList, H5AddressRespVO.class);

    }

    @Override
    public H5AddressDetailsRespVO getAddressDetailsById(Long id) {
        UmsAddress umsAddress = umsAddressMapper.selectById(id);
        if(umsAddress == null || !umsAddress.getUserId().equals(LoginHelper.getUserId()) ){
            throw new ServiceException("收货地址不存在");
        }
        return BeanUtils.toBean(umsAddress, H5AddressDetailsRespVO.class);
    }

    @Override
    public void saveAddress(H5AddressSaveReqVO h5AddressSaveReqVO) {
        UmsAddress umsAddress = BeanUtils.toBean(h5AddressSaveReqVO, UmsAddress.class);
        umsAddress.setCreateTime(new Date());
        umsAddress.setUserId(LoginHelper.getUserId());
        // 如果是默认地址，则把以前的默认地址改为非默认地址
        if (h5AddressSaveReqVO.getIsDefault()) {
            umsAddressMapper.update(
                new LambdaUpdateWrapper<UmsAddress>()
                    .set(UmsAddress::getIsDefault, false )
                    .eq(UmsAddress::getUserId,LoginHelper.getUserId())
                    .eq(UmsAddress::getIsDefault,true)
            );
        }
        umsAddressMapper.insert(umsAddress);
    }

    @Override
    public void updateAddress(H5AddressUpdateReqVO h5AddressUpdateReqVO) {
        UmsAddress umsAddress = BeanUtils.toBean(h5AddressUpdateReqVO, UmsAddress.class);
        //校验
        Long count = umsAddressMapper.selectCount(
                new LambdaQueryWrapper<UmsAddress>()
                        .eq(UmsAddress::getUserId, LoginHelper.getUserId())
                        .eq(UmsAddress::getId, h5AddressUpdateReqVO.getId())
        );
        if(count <= 0){
            throw new ServiceException("收货地址不存在");
        }

        // 如果是默认地址，则把以前的默认地址改为非默认地址
        if (h5AddressUpdateReqVO.getIsDefault()) {
            umsAddressMapper.update(
                    new LambdaUpdateWrapper<UmsAddress>()
                            .set(UmsAddress::getIsDefault, false )
                            .eq(UmsAddress::getUserId,LoginHelper.getUserId())
                            .eq(UmsAddress::getIsDefault,true)
            );
        }

        umsAddress.setUpdateTime(new Date());
        umsAddressMapper.updateById(umsAddress);
    }

    @Override
    public void deleteById(Long id) {
        umsAddressMapper.delete(
                new LambdaQueryWrapper<UmsAddress>()
                .eq(UmsAddress::getUserId,LoginHelper.getUserId())
                .eq(UmsAddress::getId,id)
        );
    }
}
